﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace ThoiTrangT2B.Admin
{
    public partial class DonHang : System.Web.UI.Page
    {
        private static int HoaDonID;
        private static int Total = 0;
        private string MyAction = MyConst.Action.Addnew;
        private string MyMaDM = "";

        #region Ham Su kien
        protected void Page_Load(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 1;
            Config();
            if (!IsPostBack)
            {
                BindDataToGrid(-1);
            }
        }

        private void Config()
        {
            if (MyMaDM == "hang")
                FormTitle.InnerText = "Quản trị Hãng sản xuất";
            else if (MyMaDM == "size")
                FormTitle.InnerText = "Quản trị danh mục Size";
            else if (MyMaDM == "yahoo")
                FormTitle.InnerText = "Quản trị danh mục hỗ trợ trực tuyến";
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("Index.aspx");
        }


        protected void grdDanhMuc_RowCommand(object sender, GridViewCommandEventArgs e)
        {

            HoaDonID = Convert.ToInt32(e.CommandArgument);


            if (e.CommandName == MyConst.Action.Update)
            {
                MultiView1.ActiveViewIndex = 0;
                Total = 0;
                GetChiTietDanhMuc(HoaDonID);

            }
            else if (e.CommandName == MyConst.Action.Delete)
            {
                try
                {
                    string sql = "DELETE FROM dbo.tblT2BHoaDon WHERE ID = @ID";
                    List<SqlParameter> lstParameter = new List<SqlParameter>();
                    lstParameter.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, HoaDonID));
                    SqlHelper.ExecuteNonQuery(CommandType.Text, sql, lstParameter);
                    sql = "DELETE FROM dbo.tblT2BCTHD WHERE HDID = @ID";
                    SqlHelper.ExecuteNonQuery(CommandType.Text, sql, lstParameter);
                    
                    BindDataToGrid(-1);
                }
                catch (Exception)
                {
                }
            }
        }

        protected void btnThem_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 0;
            //txtMaDanhMuc.Text = MyMaDM;
            //txtTenDanhMuc.Text = "";
            btnLuu.CommandName = MyConst.Action.Addnew;
        }

        protected void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                int TrangThai;
                if (rbtTMoi.Checked)
                {
                    TrangThai = 0;
                }
                else if (rbtTCho.Checked)
                {
                    TrangThai = 1;
                }
                else
                {
                    TrangThai = 2;
                }
                UpdateDanhMuc(TrangThai, HoaDonID);
                BindDataToGrid(-1);
                MultiView1.ActiveViewIndex = 1;
            }
            catch (Exception)
            {
            }
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 1;
        }
        #endregion Ham su kien

        #region Ham chuc nang

        protected void BindDataToGrid(int TrangThai)
        {
            string sql = "SELECT hd.ID,hd.Hoten,SUM((s.Gia*(100 - s.KM)/100)*ct.SL) as ThanhTien,hd.TrangThai,hd.CreateDate FROM dbo.tblT2BSanPham s INNER JOIN dbo.tblT2BCTHD ct " +
                         "ON s.ID = ct.SPID INNER JOIN dbo.tblT2BHoaDon hd ON hd.ID = ct.HDID WHERE TrangThai = @TrangThai OR @TrangThai = -1 GROUP BY hd.ID,hd.Hoten,hd.TrangThai,hd.CreateDate ORDER BY hd.CreateDate DESC";
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@TrangThai", SqlDbType.SmallInt, ParameterDirection.Input, TrangThai));
            grdDanhMuc.DataSource = SqlHelper.ExecuteDataTable(CommandType.Text, sql, arrParams);
            grdDanhMuc.DataBind();
        }

        protected void GetChiTietDanhMuc(int sID)
        {
            string sql = "SELECT * FROM dbo.tblT2BHoaDon WHERE ID = @ID";
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, sID));
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql, arrParams);
            lblHoTen.Text = dt.Rows[0]["Hoten"].ToString();
            lblEmail.Text = dt.Rows[0]["Email"].ToString();
            lblSDT.Text = dt.Rows[0]["SDT"].ToString();
            lblDiaChi.Text = dt.Rows[0]["Diachi"].ToString();
            lblGhiChu.Text = dt.Rows[0]["Ghichu"].ToString();
            int TrangThai = Convert.ToInt32(dt.Rows[0]["TrangThai"]);
            string show = "";
            if (TrangThai == 0)
            {
                show = "<font color=red>Mới</font>";
                rbtTMoi.Checked = true;
            }
            else if (TrangThai == 1)
            {
                show = "<font color=orange>Đang xử lý</font>";
                rbtTCho.Checked = true;
            }
            else
            {
                show = "<font color=green>Xử lý xong</font>";
                rbtTXong.Checked = true;
            }
            lblTrangThai.Text = show;

            dt = null;
            sql = "SELECT hd.ID,hd.Hoten,s.TenSP,ct.SL,s.Gia as Gia,(s.Gia*(100 - s.KM)/100)*ct.SL as ThanhTien FROM dbo.tblT2BSanPham s " +
                  "INNER JOIN dbo.tblT2BCTHD ct ON s.ID = ct.SPID INNER JOIN dbo.tblT2BHoaDon hd ON hd.ID = ct.HDID WHERE hd.ID = @ID";
            dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql, arrParams);
            grvGioHang.DataSource = dt;
            grvGioHang.DataBind();
            lblTotal.Text = Total.ToString("###,### VNĐ");
        }

        protected void UpdateDanhMuc(int TrangThai, int HDID)
        {
            string sql = "UPDATE dbo.tblT2BHoaDon SET TrangThai = " + TrangThai + " WHERE ID = " + HDID;
            SqlHelper.ExecuteNonQuery(CommandType.Text, sql);
        }

        public void CheckPermision()
        {
            //Dictionary<string, int> Role = MyCommon.CheckPermission(MyMaDM);
            //if (Role == null || Role["Xem"] == 0)
            //{
            //    Response.Redirect("Index.aspx?p=0");
            //}
        }
        #endregion  Ham chuc nang

        protected void rbtMoi_CheckedChanged(object sender, EventArgs e)
        {
            BindDataToGrid(0);
        }

        protected void rbtCho_CheckedChanged(object sender, EventArgs e)
        {
            BindDataToGrid(1);
        }

        protected void rbtXong_CheckedChanged(object sender, EventArgs e)
        {
            BindDataToGrid(2);
        }

        protected void grvGioHang_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            Total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ThanhTien"));
        }
    }
}
